package com.example.sinoyd.frameapplication.ProjectCodes.UI

import android.os.Bundle
import android.util.Log
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.gson
import com.example.sinoyd.frameapplication.KotlinFrame.Frame.Uitl.Networkrequestmodel
import com.example.sinoyd.frameapplication.KotlinFrame.MainActivity
import com.example.sinoyd.frameapplication.KotlinFrame.UI.BaseActivity
import com.example.sinoyd.frameapplication.ProjectCodes.Dataclass.GetPointInfo
import com.example.sinoyd.frameapplication.R
import com.sinoyd.Code.Until.Networkrequestaddress
import com.sinoyd.Code.Until.SharedPreferencesFactory
import com.sinoyd.Code.Until.showdialog
import kotlinx.android.synthetic.main.activity_os_choose.*
import okhttp3.Response
import org.jetbrains.anko.onClick
import org.jetbrains.anko.startActivity

class OsChooseActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_os_choose)
        setlisteners()
    }

    private fun setlisteners() {
        //气系统
        llosair.onClick {
            SharedPreferencesFactory.savedata(this, "OS", "air")
            if (SharedPreferencesFactory.getclass(this, "PointInfoAir", GetPointInfo.PointInfoBean::class.java) == null) {
                GetPointInfoforAir()
            } else {
                //跳转界面
                startActivity<MainActivity>()
                finish()
            }

        }
        //水系统
        lloswater.onClick {
            SharedPreferencesFactory.savedata(this, "OS", "water")
            if (SharedPreferencesFactory.getclass(this, "PointInfoWater", GetPointInfo.PointInfoBean::class.java) == null) {
                GetPointInfoforWater()
            } else {
                //跳转界面
                startActivity<MainActivity>()
                finish()
            }
        }
    }


    var getPointInfo = GetPointInfo()
    override fun requestSuccess(response: Response, TAG: String) {
        super.requestSuccess(response, TAG)
        when (TAG) {
            "PointInfoWater" -> {
                getPointInfo = gson.fromJson(responsestr, GetPointInfo::class.java)
                if (getPointInfo.pointInfo.size > 0) {
                    //存储
                    SharedPreferencesFactory.saveclass(this, "PointInfoWater", responsestr)
                } else {
                    Log.i("scj", "水系统站点未获取到")
                }
                //跳转界面
                startActivity<MainActivity>()
                finish()

            }
            "PointInfoAir" -> {
                getPointInfo = gson.fromJson(responsestr, GetPointInfo::class.java)
                if (getPointInfo.pointInfo.size > 0) {
                    //存储
                    SharedPreferencesFactory.saveclass(this, "PointInfoAir", responsestr)
                } else {
                    Log.i("scj", "气系统站点未获取到")
                }
                //跳转界面
                startActivity<MainActivity>()
                finish()
            }
        }
    }

    //气的站点信息
    fun GetPointInfoforAir() {
        showdialog("loadshow")
        var ntrequest = Networkrequestmodel()
        ntrequest.setMethod(Networkrequestmodel.GETREQUEST)
                .settag("PointInfoAir")
                .seturl(Networkrequestaddress.URL_GetPointInfo)
                .addparam("sysType", "air")
                .start(this)
    }

    //水的站点信息
    fun GetPointInfoforWater() {
        showdialog("loadshow")
        var ntrequest = Networkrequestmodel()
        ntrequest.setMethod(Networkrequestmodel.GETREQUEST)
                .settag("PointInfoWater")
                .seturl(Networkrequestaddress.URL_GetPointInfo)
                .addparam("sysType", "water")
                .start(this)
    }
}
